package com.lanou.moving.adapter;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.lanou.moving.R;
import com.lanou.moving.entity.MusicInfo;
import com.lanou.moving.fragment.SingleMusicFragment;

import java.util.List;

/**
 * Created by 梁树珍 on 15/11/5.
 */
public class LoadingAdapter extends BaseAdapter{
    private Context context;
    private List<MusicInfo> musicInfoList;
    private Handler handler;

    public LoadingAdapter(Context context, List<MusicInfo> musicInfoList) {
        this.context = context;
        this.musicInfoList = musicInfoList;
    }

    @Override
    public int getCount() {
        return musicInfoList == null ? 0 : musicInfoList.size();
    }

    @Override
    public Object getItem(int position) {
        return musicInfoList == null ? null : musicInfoList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder ;
        if (convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.loading_listview_item, null);
            holder = new ViewHolder();
            holder.load_song_name = (TextView) convertView.findViewById(R.id.load_song_name);
            holder.load_progress_number = (TextView) convertView.findViewById(R.id.load_progress_number);
            holder.download_progressbar = (ProgressBar) convertView.findViewById(R.id.download_progressbar);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.load_song_name.setText(musicInfoList.get(position).getSongName());
        handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                if (msg.what == 1){
                    holder.download_progressbar.setProgress(msg.getData().getInt("size"));
                    int progress = holder.download_progressbar.getProgress();
                    int max = holder.download_progressbar.getMax();
                    double dProgress = (double) progress / (double) (1024 * 1024);
                    double dMax = (double) max / (double) (1024 * 1024);
                    holder.load_progress_number.setText(String.format("%1.2fM/%2.2fM", dProgress, dMax));
                    if (dProgress == dMax){
                        SingleMusicFragment fragment = new SingleMusicFragment();
                        Bundle bundle = new Bundle();
                        bundle.putParcelable("musicInfo", (Parcelable) getItem(position));
                        fragment.setArguments(bundle);
                    }
                }
            }
        };
        handler.sendEmptyMessage(0);
        holder.download_progressbar.setMax((int) musicInfoList.get(position).getDuration());
        handler.sendEmptyMessage(0);
        return convertView;
    }

    private class ViewHolder{
        private TextView load_song_name, load_progress_number;
        private ProgressBar download_progressbar;
    }
}
